
/*
   CC.NET SideBar Gadget for Windows Vista 
   (C) Simone Chiaretta, 2007
   http://codeclimber.net.nz

   This project is licensed under the new BSD license.
   See the License.txt file for more information.
*/

var GadgetSettings = 
{
	init : function() 
	{
		Settings.Read();
		displayMode.value = Settings.DisplayMode;
		srvInterval.value = Settings.RefreshInterval;
		if(Settings.AutoScrollEnabled)
			autoscroll.value = "enabled";
		else
			autoscroll.value = "disabled";
		this.enableAutoScrollInterval();
		scrollInterval.value = Settings.AutoScrollInterval;
		this.RefreshProjectList();
	},
	enableAutoScrollInterval : function()
	{
	   if(autoscroll.value == "enabled")
	      scrollInterval.disabled="";
	   else
	      scrollInterval.disabled="disabled";
	},
	SettingsClosing : function(event)
	{
      if (event.closeAction == event.Action.commit)
      {	
         Settings.RefreshInterval=srvInterval.value;
         Settings.DisplayMode=displayMode.value;
         if(autoscroll.value=="enabled")
            Settings.AutoScrollEnabled=true;
         else
            Settings.AutoScrollEnabled=false;
         Settings.AutoScrollInterval=scrollInterval.value;
         
         if(Settings.ProjectList.length==0)
         {
            GadgetSettings.displayError("No project selected");
            event.cancel = true;
            return;	
         }
         
         //Ok button was clicked
         if(Settings.Validate())
         {
            Settings.Save();
			   //event.cancel = false;
			   return;
         }
         else
         {
            GadgetSettings.displayError("Validation Error, please check 'Check Interval' and 'Automatic Scrolling'");
            event.cancel = true;
            return;	
         }
      }
	},
	enableControls : function()
	{
		this.swapControls(false);
	},
	disableControls : function()
	{
		this.swapControls(true);
	},
	swapControls : function(disable)
	{
		projects.disabled=disable;
		addProject.disabled=disable;
		removeProject.disabled=disable;
	},
	openAddProjectDiv : function()
	{
	   srvUrl.value=Settings.GetLastUsedServer();
		var popupContainerDiv=document.getElementById("modalPanelContainer");
		var popupDiv=document.getElementById("modalPanel");
		var projectListDiv=document.getElementById("projectListDiv");

		popupContainerDiv.style.display="block";
		popupDiv.style.display="block";
		this.disableControls();
	},
	closeAddProjectDiv : function()
	{
		var popupContainerDiv=document.getElementById("modalPanelContainer");
		var popupDiv=document.getElementById("modalPanel");
		var projectListDiv=document.getElementById("projectListDiv");

		var projectListSelect = document.getElementById("projectsFromServer");
		this.emptySelect(projectListSelect);
		this.clearError();
		srvUrl.value="";

		popupContainerDiv.style.display="none";
		popupDiv.style.display="none";
		this.enableControls();
	},
	displayError : function(message)
	{
	   var errorMessageModalContainer=document.getElementById("errorPanelContainer");
		var errorMessageDiv=document.getElementById("errorPanel");
		var errorMessage=document.getElementById("errorMessage");
		errorMessageModalContainer.style.display="block";
		errorMessageDiv.style.display="block";
		errorMessage.innerHTML = message;		
	},
	clearError : function()
	{
	   var errorMessageModalContainer=document.getElementById("errorPanelContainer");
		var errorMessageDiv=document.getElementById("errorPanel");
		var errorMessage=document.getElementById("errorMessage");
		errorMessageModalContainer.style.display="none";
		errorMessageDiv.style.display="none";
		errorMessage.innerHTML = "";		
	},
	loadProjects : function()
	{
		this.clearError();
		var url = srvUrl.value;
		if(url.length==0)
		{
			this.displayError("Url not valid");
			return;
		}

		if(url.charAt(url.length-1)!='/')
		   url = url+'/';
		
		var proxy = new AjaxProxy();
		proxy.RequestXmlStatus(url,this.populateList,this.displayError);
	},
	emptySelect : function (select)
	{
		for(var i in select.options)
		{
			select.remove(i);
		}
	},
	populateList : function (projects)
	{
	   Settings.SetLastUsedServer(srvUrl.value);
		var projectListSelect = document.getElementById("projectsFromServer");
		GadgetSettings.emptySelect(projectListSelect);
		var addedProjects = 0;
		for(var i in projects)
		{
		   if(!SettingsContainsProject(projects[i]))
		   {
			   var option = document.createElement('option');
			   option.text=projects[i].Name;
			   option.value=projects[i].Name;
			   option.title=projects[i].Name;
			   projectListSelect.add(option);
			   addedProjects++;
			}
		}
		if(addedProjects==0)
		   GadgetSettings.displayError("All the projects have been already added.");
	},
	addProjects : function()
	{
		var projectListSelect = document.getElementById("projectsFromServer");
		if(projectListSelect.options.length==0)
		{
		   this.displayError("You have to specify a server first");
		   return;
		}
		if(projectListSelect.selectedIndex==-1)
		{
		   this.displayError("You have to select at least one project");
		   return;
		}
		
		var url = srvUrl.value;
		if(url.charAt(url.length-1)!='/')
		   url = url+'/';
		   
		for(var i=0;i<projectListSelect.options.length;i++)
		{
			if(projectListSelect.options[i].selected)
			{
				var project = new ProjectSettings(url,projectListSelect.options[i].value);
				Settings.AddProjectSettings(project);
			}
		}
		this.closeAddProjectDiv();
		this.RefreshProjectList();
	},
	RefreshProjectList: function()
	{
		var projectListSelect = document.getElementById("projects");
		GadgetSettings.emptySelect(projectListSelect);
		for(var i in Settings.ProjectList)
		{
			var option = document.createElement('option');
			option.text=Settings.ProjectList[i].projectName + " - " + Settings.ProjectList[i].serverName;
			option.title=Settings.ProjectList[i].projectName + " - " + Settings.ProjectList[i].serverName;
			option.value=Settings.ProjectList[i].projectName + "|" + Settings.ProjectList[i].serverName;
			projectListSelect.add(option);
		}
	},
	removeSelectedProject: function()
	{
	   var projectListSelect = document.getElementById("projects");
	   var selProj = projectListSelect.selectedIndex;
	   if(selProj == -1)
	      return;
	   else
	   {
	      var objParts = projectListSelect.options[selProj].value.split("|");
	      var project = new ProjectSettings(objParts[1],objParts[0]);
	      Settings.RemoveProjectSettings(project);
	      this.RefreshProjectList();
      }
	},
	moveProjectUp: function()
	{
	   var projectListSelect = document.getElementById("projects");
	   var selProj = projectListSelect.selectedIndex;
	   if(selProj == -1 || selProj == 0)
	      return;
	   else
	   {
	      Settings.SwapProjectSettings(selProj,selProj-1);
	      this.RefreshProjectList();
	      projectListSelect.selectedIndex=selProj-1;
      }
	},
	moveProjectDown: function()
	{
	   var projectListSelect = document.getElementById("projects");
	   var selProj = projectListSelect.selectedIndex;
	   if(selProj == -1 || selProj == projectListSelect.length-1)
	      return;
	   else
	   {
	      Settings.SwapProjectSettings(selProj,selProj+1);
	      this.RefreshProjectList();
	      projectListSelect.selectedIndex=selProj+1;
      }
	}
}



function SettingsContainsProject(project)
{
		var url = srvUrl.value;
		if(url.charAt(url.length-1)!='/')
		   url = url+'/';

      for(var i in Settings.ProjectList)
      {
         if(Settings.ProjectList[i].projectName==project.Name && Settings.ProjectList[i].serverUrl==url)
            return true;
      }
      return false;
}